package frame.system.web.conf;

import frame.base.Ajax;
import frame.base.BaseWeb;
import frame.base.BootstrapTable;
import frame.system.qo.ConfQo;
import frame.system.service.ConfConverterService;
import frame.system.to.ConfConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping(ConverterWeb.DIR)
public class ConverterWeb extends BaseWeb{

    public static final String DIR = "/web/system/conf/converter";

    @Autowired
    private ConfConverterService confConverterService;


    @RequestMapping("/index")
    public String index(Model model, ConfQo qo) {
        model.addAttribute("dir", DIR);
        model.addAttribute("qo", qo);
        return "system/conf/converterlist";
    }
    @RequestMapping("/table")
    @ResponseBody
    public BootstrapTable<ConfConverter> table(ConfQo qo) {
        return confConverterService.pageList(qo);
    }

    @RequestMapping("/add")
    @ResponseBody
    public Ajax add(@Valid() ConfConverter convertor, BindingResult bd) {
        try {
            super.checkFormData(bd);
            confConverterService.addSelective(convertor);
            return Ajax.success(convertor,"数据创建成功！");
        } catch (Exception e) {
            return Ajax.faild("数据创建失败：" + e.getMessage());
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Ajax delete(String[] ids) {
        try {
            List<Integer> convertorIds = strId2Integer(ids);
            int re =  confConverterService.remove(convertorIds);
            return Ajax.success(re, "数据删除成功");
        } catch (Exception e) {
            return Ajax.faild("数据删除失败：" + e.getMessage());
        }
    }

    @RequestMapping("/update")
    @ResponseBody
    public Ajax update(@Valid() ConfConverter convertor, BindingResult bd) {
        try {
            checkFormData(bd);
            int re = confConverterService.updateSelective(convertor);
            return Ajax.success(re,"数据更新成功！");
        } catch (Exception e) {
            return Ajax.faild("数据更新失败：" + e.getMessage());
        }
    }




    @RequestMapping("/get")
    @ResponseBody
    public Ajax get(Integer id) {
        try {
            ConfConverter convertor = confConverterService.selectByPrimaryKey(id);
            return Ajax.success(convertor,"数据查询成功");
        } catch (Exception e) {
            return Ajax.faild("数据查询失败：" + e.getMessage());
        }
    }

}
